

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="深海火锅店">
  <meta name="keywords" content="">
  
    <meta name="description" content="RabbitMQ 以下具体内容参考自黑马程序员课程微服务开发框架SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式微服务全技术栈课程  初识MQ同步调用微服务间基于Feign的调用就属于同步方式，存在一些问题。业务代码耦合严重，除此之外对性能影响也很严重，因为需要等待被调用的服务返回结果。 不仅如此，而且如果被调用的服务中，有某个服务挂了，则整个服务调用都会出现问题。">
<meta property="og:type" content="article">
<meta property="og:title" content="RabbitMQ">
<meta property="og:url" content="http://example.com/2024/04/03/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/Java/%E5%AD%A6%E4%B9%A0/%E6%A1%86%E6%9E%B6/RabbitMq/RabbitMq/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="RabbitMQ 以下具体内容参考自黑马程序员课程微服务开发框架SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式微服务全技术栈课程  初识MQ同步调用微服务间基于Feign的调用就属于同步方式，存在一些问题。业务代码耦合严重，除此之外对性能影响也很严重，因为需要等待被调用的服务返回结果。 不仅如此，而且如果被调用的服务中，有某个服务挂了，则整个服务调用都会出现问题。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402071726650.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402071919653.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402072322477.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402073029943.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402073427083.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402073613643.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402073847983.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402074101842.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402074226842.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402074543152.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402074637327.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402080719815.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402082842181.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402210813058.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402210928426.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402211454812.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402211730081.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402214519822.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402214633520.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402215642007.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402215706608.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402220448895.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402222040723.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240403065247801.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240403065809837.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240403071928081.png">
<meta property="og:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240403072815201.png">
<meta property="article:published_time" content="2024-04-03T00:00:00.000Z">
<meta property="article:modified_time" content="2024-04-02T23:41:02.133Z">
<meta property="article:author" content="深海火锅店">
<meta property="article:tag" content="RabbitMQ">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402071726650.png">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>RabbitMQ - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.3","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Fluid</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/background.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="RabbitMQ"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2024-04-03 08:00" pubdate>
          2024年4月3日 早上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          26k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          221 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">RabbitMQ</h1>
            
            
              <div class="markdown-body">
                
                <h1 id="RabbitMQ"><a href="#RabbitMQ" class="headerlink" title="RabbitMQ"></a>RabbitMQ</h1><blockquote>
<p>以下具体内容参考自黑马程序员课程<code>微服务开发框架SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式微服务全技术栈课程</code></p>
</blockquote>
<h2 id="初识MQ"><a href="#初识MQ" class="headerlink" title="初识MQ"></a>初识MQ</h2><h3 id="同步调用"><a href="#同步调用" class="headerlink" title="同步调用"></a>同步调用</h3><p>微服务间基于Feign的调用就属于同步方式，存在一些问题。业务代码耦合严重，除此之外对性能影响也很严重，因为需要等待被调用的服务返回结果。</p>
<p>不仅如此，而且如果被调用的服务中，有某个服务挂了，则整个服务调用都会出现问题。</p>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402071726650.png" srcset="/img/loading.gif" lazyload alt="image-20240402071726650"></p>
<h3 id="异步调用"><a href="#异步调用" class="headerlink" title="异步调用"></a>异步调用</h3><p>异步调用常见实现就是事件驱动模式</p>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402071919653.png" srcset="/img/loading.gif" lazyload alt="image-20240402071919653"></p>
<p><strong>优势</strong></p>
<blockquote>
<p>前三个解决了同步调用的问题</p>
</blockquote>
<ol>
<li><p>服务解耦，现在只需要发送成功事件即可，不需要更改业务代码</p>
</li>
<li><p>性能提高，吞吐量升高，</p>
</li>
<li><p>服务没有强依赖，无需担心级联失败问题</p>
</li>
<li><p>流量削峰，broker能起到缓冲的作用，右侧的服务则按照自身能力来获取broker的事件</p>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402072322477.png" srcset="/img/loading.gif" lazyload alt="image-20240402072322477"></p>
</li>
</ol>
<p><strong>劣势</strong></p>
<blockquote>
<p>异步通信的缺点</p>
</blockquote>
<ol>
<li>依赖于Broker的可靠性、安全性、吞吐能力</li>
<li>架构复杂了，业务没有明显的流程线，不好追踪管理</li>
</ol>
<h3 id="MQ选型"><a href="#MQ选型" class="headerlink" title="MQ选型"></a>MQ选型</h3><p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402073029943.png" srcset="/img/loading.gif" lazyload alt="image-20240402073029943"></p>
<p><strong>安装步骤省略</strong></p>
<p>rabbitmq的管理界面的端口为15672</p>
<p><a target="_blank" rel="noopener" href="http://localhost:15672/">http://localhost:15672</a></p>
<p>管理员的账号和密码是<code>guest</code></p>
<h4 id="Overview"><a href="#Overview" class="headerlink" title="Overview"></a>Overview</h4><p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402073427083.png" srcset="/img/loading.gif" lazyload alt="image-20240402073427083"></p>
<blockquote>
<p><code>Overview</code>是总览，可以看到MQ的节点信息，当前为单节点运行，并不存在集群</p>
</blockquote>
<h4 id="Connections"><a href="#Connections" class="headerlink" title="Connections"></a>Connections</h4><p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402073613643.png" srcset="/img/loading.gif" lazyload alt="image-20240402073613643"></p>
<blockquote>
<p>无论是消息的<code>发布者</code>还是消息的<code>消费者</code>都需要与MQ建立连接</p>
</blockquote>
<h4 id="Channels"><a href="#Channels" class="headerlink" title="Channels"></a>Channels</h4><p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402073847983.png" srcset="/img/loading.gif" lazyload alt="image-20240402073847983"></p>
<blockquote>
<p><code>Channels</code>是通道，在建立连接后需要创建<code>Channels</code>通道，然后消息的<code>发布者</code>和消息的<code>消费者</code>才能基于<code>Channels</code>完成消息的<code>发布</code>和<code>消费</code></p>
</blockquote>
<h4 id="Exchanges"><a href="#Exchanges" class="headerlink" title="Exchanges"></a>Exchanges</h4><p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402074101842.png" srcset="/img/loading.gif" lazyload alt="image-20240402074101842"></p>
<blockquote>
<p><code>Exchanges</code>交换机用于接收来自生产者的消息，将它们推入队列</p>
</blockquote>
<h4 id="Queues"><a href="#Queues" class="headerlink" title="Queues"></a>Queues</h4><p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402074226842.png" srcset="/img/loading.gif" lazyload alt="image-20240402074226842"></p>
<blockquote>
<p><code>Queues</code>队列用来做消息存储</p>
</blockquote>
<h4 id="Admin"><a href="#Admin" class="headerlink" title="Admin"></a>Admin</h4><p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402074543152.png" srcset="/img/loading.gif" lazyload alt="image-20240402074543152"></p>
<h4 id="MQ架构"><a href="#MQ架构" class="headerlink" title="MQ架构"></a>MQ架构</h4><p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402074637327.png" srcset="/img/loading.gif" lazyload alt="image-20240402074637327"></p>
<h3 id="常见消息模型"><a href="#常见消息模型" class="headerlink" title="常见消息模型"></a>常见消息模型</h3><blockquote>
<p><code>基本消息队列</code>和<code>工作消息队列</code>没有使用到交换机，属于简单队列模型</p>
</blockquote>
<ol>
<li>基本消息队列(BasicQueue)</li>
<li>工作消息队列(WorkQueue)</li>
<li>发布订阅( Publish、Subscribe )，又根据交换机类型不同分为三种<ol>
<li>Fanout Exchange:广播</li>
<li>Direct Exchange:路由</li>
<li>Topic Exchange:主题</li>
</ol>
</li>
</ol>
<h2 id="基本消息队列"><a href="#基本消息队列" class="headerlink" title="基本消息队列"></a>基本消息队列</h2><blockquote>
<p>这里建立的项目结构如下</p>
</blockquote>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs css">rabbitmq-demo<br>├─consumer<br>│  └─<span class="hljs-attribute">src</span><br>│      ├─<span class="hljs-selector-tag">main</span><br>│      │  └─resources<br>│      └─test<br>└─publisher<br>    └─<span class="hljs-attribute">src</span><br>        ├─<span class="hljs-selector-tag">main</span><br>        │  ├─java<br>        │  └─resources<br>        └─test<br></code></pre></td></tr></table></figure>

<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402080719815.png" srcset="/img/loading.gif" lazyload alt="image-20240402080719815"></p>
<blockquote>
<p>父级pom</p>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">project</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0&quot;</span> <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">modelVersion</span>&gt;</span>4.0.0<span class="hljs-tag">&lt;/<span class="hljs-name">modelVersion</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.example<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>rabbitmq-demo<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">name</span>&gt;</span>rabbitmq-demo<span class="hljs-tag">&lt;/<span class="hljs-name">name</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">description</span>&gt;</span>rabbitmq-demo<span class="hljs-tag">&lt;/<span class="hljs-name">description</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">packaging</span>&gt;</span>pom<span class="hljs-tag">&lt;/<span class="hljs-name">packaging</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">modules</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">module</span>&gt;</span>consumer<span class="hljs-tag">&lt;/<span class="hljs-name">module</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">module</span>&gt;</span>publisher<span class="hljs-tag">&lt;/<span class="hljs-name">module</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">modules</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">properties</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">java.version</span>&gt;</span>17<span class="hljs-tag">&lt;/<span class="hljs-name">java.version</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">project.build.sourceEncoding</span>&gt;</span>UTF-8<span class="hljs-tag">&lt;/<span class="hljs-name">project.build.sourceEncoding</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">project.reporting.outputEncoding</span>&gt;</span>UTF-8<span class="hljs-tag">&lt;/<span class="hljs-name">project.reporting.outputEncoding</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">spring-boot.version</span>&gt;</span>3.0.2<span class="hljs-tag">&lt;/<span class="hljs-name">spring-boot.version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">properties</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependencyManagement</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-dependencies<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>$&#123;spring-boot.version&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">type</span>&gt;</span>pom<span class="hljs-tag">&lt;/<span class="hljs-name">type</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>import<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependencyManagement</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">project</span>&gt;</span><br></code></pre></td></tr></table></figure>

<blockquote>
<p>publisher</p>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">project</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0&quot;</span> <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">modelVersion</span>&gt;</span>4.0.0<span class="hljs-tag">&lt;/<span class="hljs-name">modelVersion</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">parent</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.example<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>rabbitmq-demo<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">parent</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>publisher<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">name</span>&gt;</span>publisher<span class="hljs-tag">&lt;/<span class="hljs-name">name</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">description</span>&gt;</span>publisher<span class="hljs-tag">&lt;/<span class="hljs-name">description</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-amqp<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.projectlombok<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>lombok<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">optional</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-name">optional</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-starter-test<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.amqp<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-rabbit-test<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependencyManagement</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.boot<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-boot-dependencies<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>$&#123;spring-boot.version&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">type</span>&gt;</span>pom<span class="hljs-tag">&lt;/<span class="hljs-name">type</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>import<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependencyManagement</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">project</span>&gt;</span><br></code></pre></td></tr></table></figure>

<blockquote>
<p>consumer与publisher一样，改名字即可</p>
<p>发送和监听消息的示例代码如下</p>
<p>需要注意的是：<code>RabbitMQ</code>的端口是<code>5672</code>，<code>管理页面</code>端口才是<code>15672</code>，这里端口需要写<code>5672</code></p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> com.rabbitmq.client.Channel;<br><span class="hljs-keyword">import</span> com.rabbitmq.client.Connection;<br><span class="hljs-keyword">import</span> com.rabbitmq.client.ConnectionFactory;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><span class="hljs-keyword">import</span> java.util.concurrent.TimeoutException;<br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * RabbitMQ发送消息测试</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span>: 不是菜狗爱编程</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@date</span>: 2024/04/02/8:11</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@description</span>:</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">SendMessageTest</span> &#123;<br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">sendMessageTest</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> IOException, TimeoutException &#123;<br>        <span class="hljs-comment">// 1.建立连接</span><br>        <span class="hljs-type">ConnectionFactory</span> <span class="hljs-variable">factory</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ConnectionFactory</span>();<br>        <span class="hljs-comment">// 1.1.设置连接参数，分别是：主机名、端口号、vhost、用户名、密码</span><br>        factory.setHost(<span class="hljs-string">&quot;localhost&quot;</span>);<br>        factory.setPort(<span class="hljs-number">5672</span>);<br>        factory.setVirtualHost(<span class="hljs-string">&quot;/&quot;</span>);<br>        factory.setUsername(<span class="hljs-string">&quot;tong&quot;</span>);<br>        factory.setPassword(<span class="hljs-string">&quot;123456&quot;</span>);<br>        <span class="hljs-comment">// 1.2.建立连接</span><br>        <span class="hljs-type">Connection</span> <span class="hljs-variable">connection</span> <span class="hljs-operator">=</span> factory.newConnection();<br><br>        <span class="hljs-comment">// 2.创建通道Channel</span><br>        <span class="hljs-type">Channel</span> <span class="hljs-variable">channel</span> <span class="hljs-operator">=</span> connection.createChannel();<br><br>        <span class="hljs-comment">// 3.创建队列</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">queueName</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;simple.queue&quot;</span>;<br>        channel.queueDeclare(queueName, <span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>, <span class="hljs-literal">null</span>);<br><br>        <span class="hljs-comment">// 4.发送消息</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">message</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;hello, rabbitmq!&quot;</span>;<br>        channel.basicPublish(<span class="hljs-string">&quot;&quot;</span>, queueName, <span class="hljs-literal">null</span>, message.getBytes());<br>        System.out.println(<span class="hljs-string">&quot;发送消息成功：【&quot;</span> + message + <span class="hljs-string">&quot;】&quot;</span>);<br><br>        <span class="hljs-comment">// 5.关闭通道和连接</span><br>        channel.close();<br>        connection.close();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> com.rabbitmq.client.*;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><span class="hljs-keyword">import</span> java.util.concurrent.TimeoutException;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * RabbitMQ监听消息测试</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span>: 不是菜狗爱编程</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@date</span>: 2024/04/02/8:11</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@description</span>:</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">ListenMessageTest</span> &#123;<br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">listenMessageTest</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> IOException, TimeoutException &#123;<br>        <span class="hljs-comment">// 1.建立连接</span><br>        <span class="hljs-type">ConnectionFactory</span> <span class="hljs-variable">factory</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ConnectionFactory</span>();<br>        <span class="hljs-comment">// 1.1.设置连接参数，分别是：主机名、端口号、vhost、用户名、密码</span><br>        factory.setHost(<span class="hljs-string">&quot;localhost&quot;</span>);<br>        factory.setPort(<span class="hljs-number">5672</span>);<br>        factory.setVirtualHost(<span class="hljs-string">&quot;/&quot;</span>);<br>        factory.setUsername(<span class="hljs-string">&quot;tong&quot;</span>);<br>        factory.setPassword(<span class="hljs-string">&quot;123456&quot;</span>);<br>        <span class="hljs-comment">// 1.2.建立连接</span><br>        <span class="hljs-type">Connection</span> <span class="hljs-variable">connection</span> <span class="hljs-operator">=</span> factory.newConnection();<br><br>        <span class="hljs-comment">// 2.创建通道Channel</span><br>        <span class="hljs-type">Channel</span> <span class="hljs-variable">channel</span> <span class="hljs-operator">=</span> connection.createChannel();<br><br>        <span class="hljs-comment">// 3.创建队列</span><br>        <span class="hljs-type">String</span> <span class="hljs-variable">queueName</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;simple.queue&quot;</span>;<br>        channel.queueDeclare(queueName, <span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>, <span class="hljs-literal">null</span>);<br><br>        <span class="hljs-comment">// 4.订阅消息</span><br>        channel.basicConsume(queueName, <span class="hljs-literal">true</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">DefaultConsumer</span>(channel)&#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">handleDelivery</span><span class="hljs-params">(String consumerTag, Envelope envelope,</span><br><span class="hljs-params">                                       AMQP.BasicProperties properties, <span class="hljs-type">byte</span>[] body)</span> <span class="hljs-keyword">throws</span> IOException &#123;<br>                <span class="hljs-comment">// 5.处理消息</span><br>                <span class="hljs-type">String</span> <span class="hljs-variable">message</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(body);<br>                System.out.println(<span class="hljs-string">&quot;接收到消息：【&quot;</span> + message + <span class="hljs-string">&quot;】&quot;</span>);<br>            &#125;<br>        &#125;);<br>        System.out.println(<span class="hljs-string">&quot;等待接收消息。。。。&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>基本消息队列的消息发送流程:</strong></p>
<ol>
<li><p>建立connection</p>
</li>
<li><p>创建channel</p>
</li>
<li><p>利用channel声明队列</p>
</li>
<li><p>利用channel向队列发送消息</p>
</li>
</ol>
<p><strong>基本消息队列的消息接收流程:</strong></p>
<ol>
<li><p>建立connection</p>
</li>
<li><p>创建channel</p>
</li>
<li><p>利用channel声明队列</p>
</li>
<li><p>定义consumer的消费行为handleDelivery()</p>
</li>
<li><p>利用channel将消费者与队列绑定</p>
</li>
</ol>
<h2 id="SpringAMQP"><a href="#SpringAMQP" class="headerlink" title="SpringAMQP"></a>SpringAMQP</h2><p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402082842181.png" srcset="/img/loading.gif" lazyload alt="image-20240402082842181"></p>
<p>特征</p>
<ol>
<li>侦听器容器，用异步处理入站消息</li>
<li>用于发送和接收消息的RabbitTemplate</li>
<li>RabbitAdmin用于自动声明队列，交换机和绑定关系</li>
</ol>
<p>案例流程如下</p>
<ol>
<li>在父工程中引入spring-amqp的依赖</li>
<li>在publisher服务中利用RabbitTemplate发送消息到simple.queue这个队列</li>
<li>在consumer服务中编写消费逻辑，绑定simple.queue这个队列</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">import</span> com.rabbitmq.client.Channel;<br><span class="hljs-keyword">import</span> com.rabbitmq.client.Connection;<br><span class="hljs-keyword">import</span> com.rabbitmq.client.ConnectionFactory;<br><span class="hljs-keyword">import</span> org.junit.jupiter.api.Test;<br><span class="hljs-keyword">import</span> org.springframework.amqp.rabbit.core.RabbitTemplate;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.boot.test.context.SpringBootTest;<br><br><span class="hljs-keyword">import</span> java.io.IOException;<br><span class="hljs-keyword">import</span> java.util.concurrent.TimeoutException;<br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * RabbitMQ发送消息测试</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span>: 不是菜狗爱编程</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@date</span>: 2024/04/02/8:11</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@description</span>:</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">SendMessageTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> RabbitTemplate rabbitTemplate;<br><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">sendMessageWithRabbitMqTemplateTest</span><span class="hljs-params">()</span>&#123;<br>        String queueName=<span class="hljs-string">&quot;simple.queue&quot;</span>;<br>        String message=<span class="hljs-string">&quot;你好，这是【RabbitTemplate】发送的消息&quot;</span>;<br>        rabbitTemplate.convertAndSend(queueName,message);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>application.properties</p>
</blockquote>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-comment"># 主机名 192.168.XXX.XXX</span><br><span class="hljs-attr">spring.rabbitmq.host</span>=<span class="hljs-string">localhost</span><br><span class="hljs-comment"># 端口</span><br><span class="hljs-attr">spring.rabbitmq.port</span>=<span class="hljs-string">5672</span><br><span class="hljs-comment"># 虚拟主机</span><br><span class="hljs-attr">spring.rabbitmq.virtual-host</span>=<span class="hljs-string">/</span><br><span class="hljs-attr">spring.rabbitmq.username</span>=<span class="hljs-string">tong</span><br><span class="hljs-attr">spring.rabbitmq.password</span>=<span class="hljs-string">123456</span><br></code></pre></td></tr></table></figure>

<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402210813058.png" srcset="/img/loading.gif" lazyload alt="image-20240402210813058"></p>
<blockquote>
<p>点击<code>simple.queue</code></p>
</blockquote>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402210928426.png" srcset="/img/loading.gif" lazyload alt="image-20240402210928426"></p>
<blockquote>
<p>consumer工程的properties也同样配置</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@Component</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">RabbitMqListener</span> &#123;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 简单队列侦听器</span><br><span class="hljs-comment">     * 消息发送者发送的是字符串，所以这边也适用字符串来接收</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> message 消息</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@RabbitListener(queues = &#123;&quot;simple.queue&quot;&#125;)</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">simpleQueueListener</span><span class="hljs-params">(String message)</span>&#123;<br>        log.info(<span class="hljs-string">&quot;接收到的消息为:&#123;&#125;&quot;</span>,message);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>启动consumer工程后接收到消息</p>
</blockquote>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402211454812.png" srcset="/img/loading.gif" lazyload alt="image-20240402211454812"></p>
<blockquote>
<p>发现<code>消息消失了</code>，因为这是MQ的机制，<code>阅后即焚</code>，消息不可重复消费</p>
</blockquote>
<h2 id="工作消息队列"><a href="#工作消息队列" class="headerlink" title="工作消息队列"></a>工作消息队列</h2><p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402211730081.png" srcset="/img/loading.gif" lazyload alt="image-20240402211730081"></p>
<blockquote>
<p>两个消费者共同消费生产者发布的消息，由于消息<code>阅后即焚</code>的特性，所以只能消费者1消费一部分消息，消费者2消费一部分消息<br>但是当发布的消息过多，两个消费者的能力不足，则多余的消息就会堆积在<code>队列</code>里<br>当<code>队列</code>存储的<code>消息</code>达到上限时，则会发生消息丢失</p>
<p>**<code>工作队列</code>**，可以提高消息处理速度，避免队列消息堆积</p>
</blockquote>
<p><strong>实现思路如下</strong></p>
<ol>
<li>在publisher服务中定义测试方法，每秒产生50条消息，发送到simple.queue的</li>
<li>在consumer服务中定义两个消息监听者，都监听simple.queue队列</li>
<li>消费者1每秒处理50条消息，消费者2每秒处理10条消息</li>
</ol>
<p>所以照理来说，消费者应该能在<code>1s</code>内<code>消费完</code>消息</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">SendMessageTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> RabbitTemplate rabbitTemplate;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 使用工作队列测试发送消息</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@throws</span> InterruptedException 中断异常</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">sendMessageWithWorkQueueTest</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> InterruptedException &#123;<br>        String queueName=<span class="hljs-string">&quot;simple.queue&quot;</span>;<br>        String message=<span class="hljs-string">&quot;你好，这是消息&quot;</span>;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">50</span>; i++) &#123;<br>            rabbitTemplate.convertAndSend(queueName,message+i);<br>            Thread.sleep(<span class="hljs-number">20</span>);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@Component</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">RabbitMqListener</span> &#123;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 工作队列侦听器1</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> message 消息</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@RabbitListener(queues = &#123;&quot;simple.queue&quot;&#125;)</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">workQueueListener1</span><span class="hljs-params">(String message)</span> <span class="hljs-keyword">throws</span> InterruptedException &#123;<br>        log.info(<span class="hljs-string">&quot;工作队列侦听器1:接收到的消息为:&#123;&#125;&quot;</span>,message);<br>        Thread.sleep(<span class="hljs-number">20</span>);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 工作队列侦听器2</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> message 消息</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@RabbitListener(queues = &#123;&quot;simple.queue&quot;&#125;)</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">workQueueListener2</span><span class="hljs-params">(String message)</span> <span class="hljs-keyword">throws</span> InterruptedException &#123;<br>        log.info(<span class="hljs-string">&quot;工作队列侦听器2:接收到的消息为:&#123;&#125;&quot;</span>,message);<br>        Thread.sleep(<span class="hljs-number">200</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>打印日志如下</p>
</blockquote>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><code class="hljs stylus"><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">56.431</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">0</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">56.456</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">1</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">56.518</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">3</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">56.580</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">5</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">56.641</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">2</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">56.641</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">7</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">56.705</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">9</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">56.767</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">11</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">56.830</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">13</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">56.845</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">4</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">56.893</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">15</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">56.956</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">17</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.018</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">19</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.049</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">6</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.081</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">21</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.145</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">23</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.208</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">25</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.253</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">8</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.269</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">27</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.333</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">29</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.397</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">31</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.458</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">10</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.460</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">33</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.522</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">35</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.584</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">37</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.651</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">39</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.662</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">12</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.710</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">41</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.775</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">43</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.837</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">45</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.867</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">14</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.900</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">47</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">57.963</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#1-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">1</span>:接收到的消息为:你好，这是消息<span class="hljs-number">49</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">58.070</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">16</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">58.276</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">18</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">58.481</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">20</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">58.687</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">22</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">58.889</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">24</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">59.096</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">26</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">59.301</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">28</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">59.507</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">30</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">59.712</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">32</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">31</span>:<span class="hljs-number">59.917</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">34</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">32</span>:<span class="hljs-number">00.119</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">36</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">32</span>:<span class="hljs-number">00.321</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">38</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">32</span>:<span class="hljs-number">00.525</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">40</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">32</span>:<span class="hljs-number">00.730</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">42</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">32</span>:<span class="hljs-number">00.946</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">44</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">32</span>:<span class="hljs-number">01.150</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">46</span><br><span class="hljs-number">2024</span>-<span class="hljs-number">04</span>-<span class="hljs-number">02</span>T21:<span class="hljs-number">32</span>:<span class="hljs-number">01.353</span>+<span class="hljs-number">08</span>:<span class="hljs-number">00</span>  INFO <span class="hljs-number">9600</span> --- <span class="hljs-selector-attr">[ntContainer#0-1]</span> c<span class="hljs-selector-class">.e</span><span class="hljs-selector-class">.consumer</span><span class="hljs-selector-class">.listener</span><span class="hljs-selector-class">.RabbitMqListener</span>   : 工作队列侦听器<span class="hljs-number">2</span>:接收到的消息为:你好，这是消息<span class="hljs-number">48</span><br></code></pre></td></tr></table></figure>

<blockquote>
<p>根据日志打印的时间得知，消息处理的时间为<code>5s</code>，但我们预计的时间是<code>1s</code></p>
<p>这是因为由于rabbitmq的<code>预取机制</code>，会在消息消费前先平分获取到消息，但消息被消费者A获取后，其他的消费者就无法再消费这些消息了</p>
<p>所以这里可以看到<code>工作队列侦听器1</code>和<code>工作队列侦听器2</code>都各自消费了<code>25</code>条消息，且<code>工作队列侦听器1</code>消费的是<code>奇数</code>，<code>工作队列侦听器2</code>则是<code>偶数</code></p>
<p>添加<code>如下配置</code>即可解决</p>
</blockquote>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-comment"># 应用服务 WEB 访问端口</span><br><span class="hljs-attr">server.port</span>=<span class="hljs-string">8090</span><br><span class="hljs-comment"></span><br><span class="hljs-comment"># 主机名 192.168.XXX.XXX</span><br><span class="hljs-attr">spring.rabbitmq.host</span>=<span class="hljs-string">localhost</span><br><span class="hljs-comment"># 端口</span><br><span class="hljs-attr">spring.rabbitmq.port</span>=<span class="hljs-string">5672</span><br><span class="hljs-comment"># 虚拟主机</span><br><span class="hljs-attr">spring.rabbitmq.virtual-host</span>=<span class="hljs-string">/</span><br><span class="hljs-attr">spring.rabbitmq.username</span>=<span class="hljs-string">tong</span><br><span class="hljs-attr">spring.rabbitmq.password</span>=<span class="hljs-string">123456</span><br><span class="hljs-comment"># 每次只取一条消息，处理完成再获取下一条</span><br><span class="hljs-attr">spring.rabbitmq.listener.simple.prefetch</span>=<span class="hljs-string">1</span><br></code></pre></td></tr></table></figure>

<h2 id="发布、订阅"><a href="#发布、订阅" class="headerlink" title="发布、订阅"></a>发布、订阅</h2><h3 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h3><blockquote>
<p>发布订阅模式与之前案例的区别就是允许将同一消息发送给多个消费者。实现方式是加入了exchange (交换机)。</p>
</blockquote>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402214519822.png" srcset="/img/loading.gif" lazyload alt="image-20240402214519822"></p>
<p>常见exchange类型包括:</p>
<ol>
<li>Fanout: 广播</li>
<li>Direct: 路由</li>
<li>Topic: 话题</li>
</ol>
<p><strong><code>注意</code></strong>: exchange负责消息路由，而不负责存储，路由失败则消息丢失</p>
<h3 id="Fanout-发布订阅"><a href="#Fanout-发布订阅" class="headerlink" title="Fanout 发布订阅"></a>Fanout 发布订阅</h3><blockquote>
<p>Fanout Exchange会将接收到的消息路由到每一个跟其绑定的queue</p>
</blockquote>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402214633520.png" srcset="/img/loading.gif" lazyload alt="image-20240402214633520"></p>
<p><strong>实现思路如下:</strong></p>
<ol>
<li>在consumer服务中，利用代码声明队列、交换机，并将两者绑定</li>
<li>在consumer服务中，编写两个消费者方法，分别监听<code>fanout.queue1</code>和<code>fanout.queue2</code></li>
<li>在publisher中编写测试方法，向<code>fanout.exchange</code>发送消息</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * FanoutExchange配置</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span>: 不是菜狗爱编程</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@date</span>: 2024/04/02/21:49</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@description</span>:</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Configuration</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">FanoutExchangeConfig</span> &#123;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * fanout交换机</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> &#123;<span class="hljs-doctag">@link</span> FanoutExchange&#125;</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> FanoutExchange <span class="hljs-title function_">fanoutExchange</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">FanoutExchange</span>(<span class="hljs-string">&quot;fanout.exchange&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * fanout队列1</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> &#123;<span class="hljs-doctag">@link</span> Queue&#125;</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> Queue <span class="hljs-title function_">fanoutQueue1</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Queue</span>(<span class="hljs-string">&quot;fanoutQueue1&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * fanout队列2</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> &#123;<span class="hljs-doctag">@link</span> Queue&#125;</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> Queue <span class="hljs-title function_">fanoutQueue2</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Queue</span>(<span class="hljs-string">&quot;fanoutQueue2&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 绑定队列1</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> fanoutQueue1   扇出队列1</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> fanoutExchange 扇出交换</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> &#123;<span class="hljs-doctag">@link</span> Binding&#125;</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> Binding <span class="hljs-title function_">buildingQueue1</span><span class="hljs-params">(Queue fanoutQueue1, FanoutExchange fanoutExchange)</span> &#123;<br>        <span class="hljs-keyword">return</span> BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 绑定队列2</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> fanoutQueue2   扇出队列1</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> fanoutExchange 扇出交换</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> &#123;<span class="hljs-doctag">@link</span> Binding&#125;</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> Binding <span class="hljs-title function_">buildingQueue2</span><span class="hljs-params">(Queue fanoutQueue2, FanoutExchange fanoutExchange)</span> &#123;<br>        <span class="hljs-keyword">return</span> BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402215642007.png" srcset="/img/loading.gif" lazyload alt="image-20240402215642007"></p>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402215706608.png" srcset="/img/loading.gif" lazyload alt="image-20240402215706608"></p>
<blockquote>
<p>配置<code>交换机</code>和<code>队列</code>以及它们的<code>绑定关系</code></p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Configuration</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">FanoutExchangeConfig</span> &#123;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * fanout交换机</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> &#123;<span class="hljs-doctag">@link</span> FanoutExchange&#125;</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> FanoutExchange <span class="hljs-title function_">fanoutExchange</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">FanoutExchange</span>(<span class="hljs-string">&quot;fanout.exchange&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * fanout队列1</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> &#123;<span class="hljs-doctag">@link</span> Queue&#125;</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> Queue <span class="hljs-title function_">fanoutQueue1</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Queue</span>(<span class="hljs-string">&quot;fanoutQueue1&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * fanout队列2</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> &#123;<span class="hljs-doctag">@link</span> Queue&#125;</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> Queue <span class="hljs-title function_">fanoutQueue2</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Queue</span>(<span class="hljs-string">&quot;fanoutQueue2&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 绑定队列1</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> fanoutQueue1   扇出队列1</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> fanoutExchange 扇出交换</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> &#123;<span class="hljs-doctag">@link</span> Binding&#125;</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> Binding <span class="hljs-title function_">buildingQueue1</span><span class="hljs-params">(Queue fanoutQueue1, FanoutExchange fanoutExchange)</span> &#123;<br>        <span class="hljs-keyword">return</span> BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 绑定队列2</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> fanoutQueue2   扇出队列1</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> fanoutExchange 扇出交换</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> &#123;<span class="hljs-doctag">@link</span> Binding&#125;</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> Binding <span class="hljs-title function_">buildingQueue2</span><span class="hljs-params">(Queue fanoutQueue2, FanoutExchange fanoutExchange)</span> &#123;<br>        <span class="hljs-keyword">return</span> BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@Component</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">RabbitMqListener</span> &#123;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * fanout队列侦听器1</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> message 消息</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@RabbitListener(queues = &#123;&quot;fanoutQueue1&quot;&#125;)</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">fanoutQueueListener1</span><span class="hljs-params">(String message)</span>&#123;<br>        log.info(<span class="hljs-string">&quot;fanout队列侦听器1:接收到的消息为:&#123;&#125;&quot;</span>,message);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * fanout队列侦听器2</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> message 消息</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@RabbitListener(queues = &#123;&quot;fanoutQueue2&quot;&#125;)</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">fanoutQueueListener2</span><span class="hljs-params">(String message)</span>&#123;<br>        log.info(<span class="hljs-string">&quot;fanout队列侦听器2:接收到的消息为:&#123;&#125;&quot;</span>,message);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">SendMessageTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> RabbitTemplate rabbitTemplate;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 使用Fanout交换机测试发送消息</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@throws</span> InterruptedException 中断异常</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">sendMessageWithFanoutExchangeTest</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> InterruptedException &#123;<br>        String exchangeName=<span class="hljs-string">&quot;fanout.exchange&quot;</span>;<br>        String message=<span class="hljs-string">&quot;你好，这是消息&quot;</span>;<br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">50</span>; i++) &#123;<br>            rabbitTemplate.convertAndSend(exchangeName,<span class="hljs-string">&quot;&quot;</span>,message+i);<br>            Thread.sleep(<span class="hljs-number">20</span>);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="DirectExchange"><a href="#DirectExchange" class="headerlink" title="DirectExchange"></a>DirectExchange</h3><blockquote>
<p>Direct Exchange会将接收到的消息根据规则路由到指定的Queue,因此称为路由模式(routes) 。</p>
<p>每一个Queue都与Exchange设置一个BindingKey</p>
<p>发布者发送消息时，指定消息的RoutingKey</p>
<p>Exchange将消息路由到BindingKey与消息RoutingKey一致的队列</p>
</blockquote>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402220448895.png" srcset="/img/loading.gif" lazyload alt="image-20240402220448895"></p>
<p>**<code>需要注意</code>**：一个队列，可以绑定多个<code>key</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@Component</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">RabbitMqListener</span> &#123;<br>   <br>	<span class="hljs-comment">/**</span><br><span class="hljs-comment">     * direct队列侦听器1</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> message 消息</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@RabbitListener(bindings = @QueueBinding(</span><br><span class="hljs-meta">            value = @Queue(&quot;directQueue1&quot;),</span><br><span class="hljs-meta">            exchange = @Exchange(value = &quot;direct.exchange&quot;,type = ExchangeTypes.DIRECT),</span><br><span class="hljs-meta">            key = &#123;&quot;red&quot;,&quot;blue&quot;&#125;</span><br><span class="hljs-meta">    ))</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">directQueueListener1</span><span class="hljs-params">(String message)</span>&#123;<br>        log.info(<span class="hljs-string">&quot;direct队列侦听器1:接收到的消息为:&#123;&#125;&quot;</span>,message);<br>    &#125;<br><br>	<span class="hljs-comment">/**</span><br><span class="hljs-comment">     * direct队列侦听器2</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> message 消息</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@RabbitListener(bindings = @QueueBinding(</span><br><span class="hljs-meta">            value = @Queue(&quot;directQueue2&quot;),</span><br><span class="hljs-meta">            exchange = @Exchange(value = &quot;direct.exchange&quot;,type = ExchangeTypes.DIRECT),</span><br><span class="hljs-meta">            key = &#123;&quot;yellow&quot;,&quot;blue&quot;&#125;</span><br><span class="hljs-meta">    ))</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">directQueueListener2</span><span class="hljs-params">(String message)</span>&#123;<br>        log.info(<span class="hljs-string">&quot;direct队列侦听器2:接收到的消息为:&#123;&#125;&quot;</span>,message);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>启动<code>consumer</code>项目，前往rabbitmq网页。发现<code>direct.exchange</code>交换机和<code>directQueue1</code>、<code>directQueue2</code>队列已经声明了</p>
<p>查看绑定关系</p>
</blockquote>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240402222040723.png" srcset="/img/loading.gif" lazyload alt="image-20240402222040723"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">SendMessageTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> RabbitTemplate rabbitTemplate;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 使用Direct交换机测试发送消息</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@throws</span> InterruptedException 中断异常</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">sendMessageWithDirectExchangeTest</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">exchangeName</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;direct.exchange&quot;</span>;<br>        String[] routingKeys=&#123;<span class="hljs-string">&quot;yellow&quot;</span>,<span class="hljs-string">&quot;red&quot;</span>,<span class="hljs-string">&quot;blue&quot;</span>&#125;;<br>        <span class="hljs-keyword">for</span> (String routingKey : routingKeys) &#123;<br>            rabbitTemplate.convertAndSend(exchangeName, routingKey, <span class="hljs-string">&quot;消息是:&quot;</span>+routingKey);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>日志打印如下</p>
</blockquote>
<figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">direct</span>队列侦听器<span class="hljs-number">2</span>:接收到的消息为:消息是:yellow<br><span class="hljs-attribute">direct</span>队列侦听器<span class="hljs-number">1</span>:接收到的消息为:消息是:red<br><span class="hljs-attribute">direct</span>队列侦听器<span class="hljs-number">1</span>:接收到的消息为:消息是:blue<br><span class="hljs-attribute">direct</span>队列侦听器<span class="hljs-number">2</span>:接收到的消息为:消息是:blue<br></code></pre></td></tr></table></figure>

<h3 id="TopicExchange"><a href="#TopicExchange" class="headerlink" title="TopicExchange"></a>TopicExchange</h3><blockquote>
<p><code>TopicExchange</code>与DirectExchange类似， 区别在于routingKey必须是多个单词的列表,并且以<code>.</code>分割。</p>
<p>Queue与Exchange指定<code>BindingKey</code>时可以使用通配符:<br><code>#</code>:代指0个或多个单词<br><code>*</code>:代指一个单词</p>
</blockquote>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240403065247801.png" srcset="/img/loading.gif" lazyload alt="image-20240403065247801"></p>
<p><strong>实现思路如下</strong></p>
<ol>
<li>并利用@RabbitListener声明Exchange、Queue、RoutingKey</li>
<li>在consumer服务中，编写两个消费者方法,分别监听<code>topic.queue1</code>和<code>topic.queue2</code></li>
<li>在publisher中编写测试方法，向<code>topic.exchange</code>发送消息</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * RabbitMQ 侦听器</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span>: 不是菜狗爱编程</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@date</span>: 2024/04/02/21:11</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@description</span>:</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@Component</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">RabbitMqListener</span> &#123;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 主题队列侦听器1</span><br><span class="hljs-comment">     * 接收 china 的一切信息</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> message 消息</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@RabbitListener(bindings = @QueueBinding(</span><br><span class="hljs-meta">            value = @Queue(&quot;topicQueue1&quot;),</span><br><span class="hljs-meta">            exchange = @Exchange(value = &quot;topic.exchange&quot;,type = ExchangeTypes.TOPIC),</span><br><span class="hljs-meta">            key = &quot;china.#&quot;</span><br><span class="hljs-meta">    ))</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">topicQueueListener1</span><span class="hljs-params">(String message)</span>&#123;<br>        log.info(<span class="hljs-string">&quot;topic队列侦听器1:接收到的消息为:&#123;&#125;&quot;</span>,message);<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 主题队列侦听器2</span><br><span class="hljs-comment">     * 接收一切 news 信息</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> message 消息</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@RabbitListener(bindings = @QueueBinding(</span><br><span class="hljs-meta">            value = @Queue(&quot;topicQueue2&quot;),</span><br><span class="hljs-meta">            exchange = @Exchange(value = &quot;topic.exchange&quot;,type = ExchangeTypes.TOPIC),</span><br><span class="hljs-meta">            key = &quot;#.news&quot;</span><br><span class="hljs-meta">    ))</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">topicQueueListener2</span><span class="hljs-params">(String message)</span>&#123;<br>        log.info(<span class="hljs-string">&quot;topic队列侦听器2:接收到的消息为:&#123;&#125;&quot;</span>,message);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>启动项目后，确认绑定关系</p>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240403065809837.png" srcset="/img/loading.gif" lazyload alt="image-20240403065809837"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * RabbitMQ发送消息测试</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span>: 不是菜狗爱编程</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@date</span>: 2024/04/02/8:11</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@description</span>:</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">SendMessageTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> RabbitTemplate rabbitTemplate;<br>    <br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 使用Topic交换机测试发送消息</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">sendMessageWithTopicExchangeTest</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-type">String</span> <span class="hljs-variable">exchangeName</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;topic.exchange&quot;</span>;<br>        String[] routingKeys=&#123;<span class="hljs-string">&quot;china.weather&quot;</span>,<span class="hljs-string">&quot;china.news&quot;</span>,<span class="hljs-string">&quot;Japan.weather&quot;</span>,<span class="hljs-string">&quot;Japan.news&quot;</span>&#125;;<br>        <span class="hljs-keyword">for</span> (String routingKey : routingKeys) &#123;<br>            rabbitTemplate.convertAndSend(exchangeName, routingKey, <span class="hljs-string">&quot;消息是:&quot;</span>+routingKey);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="消息转换器"><a href="#消息转换器" class="headerlink" title="消息转换器"></a>消息转换器</h2><blockquote>
<p>说明:在SpringAMQP的发送方法中，接收消息的类型是Object,也就是说我们可以发送任意对象类型的消息，SpringAMQP会帮我们序列化为字节后发送。</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Configuration</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">FanoutExchangeConfig</span> &#123;<br>    <br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 对象队列</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> &#123;<span class="hljs-doctag">@link</span> Queue&#125;</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> Queue <span class="hljs-title function_">objectQueue</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Queue</span>(<span class="hljs-string">&quot;objectQueue&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@Component</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">RabbitMqListener</span> &#123;<br><br>    <span class="hljs-meta">@RabbitListener(queues = &#123;&quot;objectQueue&quot;&#125;)</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">objectQueueListener</span><span class="hljs-params">(Object object)</span>&#123;<br>        log.info(<span class="hljs-string">&quot;objectQueue队列侦听器:接收到的消息为:&#123;&#125;&quot;</span>,object);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>这里将consumer启动之后，就会创建<code>objectQueue</code>队列</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@SpringBootTest</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">SendMessageTest</span> &#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> RabbitTemplate rabbitTemplate;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 发送对象消息测试</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-keyword">void</span> <span class="hljs-title function_">sendObjectMessageTest</span><span class="hljs-params">()</span> &#123;<br>        String queueName=<span class="hljs-string">&quot;objectQueue&quot;</span>;<br>        Map&lt;String, Object&gt; map = <span class="hljs-keyword">new</span> <span class="hljs-title class_">HashMap</span>&lt;&gt;();<br>        map.put(<span class="hljs-string">&quot;name&quot;</span>,<span class="hljs-string">&quot;马小跳&quot;</span>);<br>        map.put(<span class="hljs-string">&quot;gender&quot;</span>,<span class="hljs-string">&quot;男&quot;</span>);<br>        map.put(<span class="hljs-string">&quot;age&quot;</span>,<span class="hljs-string">&quot;18&quot;</span>);<br>        rabbitTemplate.convertAndSend(queueName,map);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>消费消息的日志打印如下</p>
</blockquote>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs routeros">objectQueue队列侦听器:接收到的消息为:(Body:<span class="hljs-string">&#x27;[serialized object]&#x27;</span> MessageProperties [headers=&#123;&#125;, <span class="hljs-attribute">contentType</span>=application/x-java-serialized-object, <span class="hljs-attribute">contentLength</span>=0, <span class="hljs-attribute">receivedDeliveryMode</span>=PERSISTENT, <span class="hljs-attribute">priority</span>=0, <span class="hljs-attribute">redelivered</span>=<span class="hljs-literal">false</span>, <span class="hljs-attribute">receivedExchange</span>=, <span class="hljs-attribute">receivedRoutingKey</span>=objectQueue, <span class="hljs-attribute">deliveryTag</span>=1, <span class="hljs-attribute">consumerTag</span>=amq.ctag-6jbSNzh0393W9MP8GZ1z-w, <span class="hljs-attribute">consumerQueue</span>=objectQueue])<br></code></pre></td></tr></table></figure>

<blockquote>
<p>这次将<code>consumer</code>项目关闭，我们重新发送信息，去<code>rabbitmq</code>管理页面看一看消息</p>
</blockquote>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240403071928081.png" srcset="/img/loading.gif" lazyload alt="image-20240403071928081"></p>
<blockquote>
<p>rabbitmq原生只支持字节，但是spring允许我们发送对象，这是因为使用了jdk序列化，但是这种序列化有缺点</p>
<ol>
<li>性能较差</li>
<li>安全性问题，容易出现注入问题</li>
<li>数据长度太长了（消息体越大，传输消息的速度越慢，而且占用额外的内存空间）</li>
</ol>
<p><code>如何重新设定序列化的方式？</code></p>
<p>Spring的对消息对象的处理是由<code>org.springframework.amqp.support.converter.MessageConverter</code>来处理的。</p>
<p>而默认实现是<code>SimpleMessageConverter</code>,基于JDK的ObjectOutputStream完成序列化。</p>
<p>如果要修改只需要定义一个MessageConverter类型的Bean即可。推荐用JSON方式序列化</p>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.fasterxml.jackson.core<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>jackson-databind<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Configuration</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">RabbitMqConfig</span> &#123;<br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 采用json的方式将消息序列化</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> &#123;<span class="hljs-doctag">@link</span> MessageConverter&#125;</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-keyword">public</span> MessageConverter <span class="hljs-title function_">messageConverter</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Jackson2JsonMessageConverter</span>();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>重新发送消息，前往rabbitmq管理页面查看</p>
</blockquote>
<p><img src="https://gitee.com/tongstyle/blog-img/raw/master/img/image-20240403072815201.png" srcset="/img/loading.gif" lazyload alt="image-20240403072815201"></p>
<blockquote>
<p>消息消费也需要配置<code>jackson</code>依赖、<code>RabbitMqConfig</code>，上面代码复制粘贴即可，这里不再重复</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * RabbitMQ 侦听器</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span>: 不是菜狗爱编程</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@date</span>: 2024/04/02/21:11</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@description</span>:</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Slf4j</span><br><span class="hljs-meta">@Component</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">RabbitMqListener</span> &#123;<br>    <br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 对象队列侦听器</span><br><span class="hljs-comment">     *</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> map 对象</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-meta">@RabbitListener(queues = &#123;&quot;objectQueue&quot;&#125;)</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">objectQueueListener</span><span class="hljs-params">(Map&lt;String,Object&gt; map)</span>&#123;<br>        log.info(<span class="hljs-string">&quot;objectQueue队列侦听器:接收到的消息为:&#123;&#125;&quot;</span>,map);<br>        <span class="hljs-keyword">for</span> (Map.Entry&lt;String, Object&gt; entry : map.entrySet()) &#123;<br>            System.out.println(<span class="hljs-string">&quot;entry = &quot;</span> + entry);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>总结</strong></p>
<p>SpringAMQP中消息的序列化和反序列化是怎么实现的?</p>
<ol>
<li>利用MessageConverter实现的，默认是JDK的序列化</li>
<li>注意发送方与接收方必须使用相同的MessageConverter</li>
</ol>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/RabbitMQ/" class="category-chain-item">RabbitMQ</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/RabbitMQ/">#RabbitMQ</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>RabbitMQ</div>
      <div>http://example.com/2024/04/03/编程语言/Java/学习/框架/RabbitMq/RabbitMq/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>深海火锅店</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2024年4月3日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2024/04/10/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/Java/%E5%AD%A6%E4%B9%A0/%E6%A1%86%E6%9E%B6/Sentinel/Sentinel/" title="Sentinel">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Sentinel</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2024/04/01/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/Java/%E5%AD%A6%E4%B9%A0/%E6%A1%86%E6%9E%B6/Elasticsearct/Elasticsearct%E5%A4%8D%E4%B9%A0/" title="elasticsearch复习">
                        <span class="hidden-mobile">elasticsearch复习</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  
<script src="//cdn.jsdelivr.net/gh/EmoryHuang/BlogBeautify@1.1/DynamicRibbon.min.js"></script>



<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
